package cqrtplm.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.common.model.dto.search.SearchConditionDTO;
import com.hustcad.plm.pdm.common.model.vo.tableview.TableViewDescriptorVO;
import com.hustcad.plm.pdm.common.service.tableview.TyplmCommonTableViewService;
import com.hustcad.plm.pdm.doc.dto.CreateDocInputDTO;
import com.hustcad.plm.pdm.doc.dto.Document;
import com.hustcad.plm.pdm.doc.vo.DocumentVO;
import com.hustcad.plm.pdm.folder.mapper.TyplmSubfolderMapper;
import com.hustcad.plm.pdm.folder.model.dto.HyperlinkDTO;
import com.hustcad.plm.pdm.folder.model.dto.filter.TySubFolderFilterDTO;
import com.hustcad.plm.pdm.folder.model.dto.filter.TySubFolderFilterQuery;
import com.hustcad.plm.pdm.folder.model.dto.filter.TyplmFolderObjDTO;
import com.hustcad.plm.pdm.folder.model.dto.move.BatchMoveOidParamDTO;
import com.hustcad.plm.pdm.folder.model.dto.move.MoveOidParamDTO;
import com.hustcad.plm.pdm.folder.model.vo.SubfolderVO;
import com.hustcad.plm.pdm.folder.model.vo.SubfolderWebVO;
import com.hustcad.plm.pdm.folder.service.TyplmCommonMoveService;
import com.hustcad.plm.pdm.folder.service.TyplmHyperlinkService;
import com.hustcad.plm.pdm.folder.service.TyplmSubFolderLinkService;
import com.hustcad.plm.pdm.folder.service.search.TyplmSubFilterObjectService;
import com.hustcad.plm.pdm.iba.service.TyplmAttributeValueService;
import com.hustcad.plm.pdm.middle.caddoc.model.dto.InsertCadDocDTO;
import com.hustcad.plm.pdm.partbom.dto.InsertPartDTO;
import com.hustcad.plm.pdm.partbom.vo.PdmPartInfoVO;
import com.hustcad.plm.pdm.type.service.TyplmTypeService;
import com.hustcad.plm.pdm.user.service.TyplmUserService;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainerView;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import com.ty.basic.constant.TableTypeConstrant;
import com.ty.basic.entity.IdentifierEntity;
import com.ty.basic.entity.folder.SubfolderDO;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.service.snow.SnowflakeIdComponent;
import com.ty.basic.vo.AttributeVO;
import com.ty.basic.vo.TyAttributeDefMapVO;
import cqrtplm.dto.PartNumberDTO;
import cqrtplm.entity.LifeCycleStateDO;
import cqrtplm.entity.ObjectStorageDO;
import cqrtplm.mapper.CheckDocMapper;
import cqrtplm.mapper.StoragePlanningMapper;
import cqrtplm.mapper.RTPartMapper;
import cqrtplm.mapper.RTTypeClassMapper;
import cqrtplm.service.DocFolderOperationService;
import cqrtplm.service.ObjectStorageService;
import cqrtplm.vo.DocxxxxVO;
import cqrtplm.vo.EBOMVO;
import cqrtplm.vo.ModelStatisticsVO;
import cqrtplm.vo.PDFProjectVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class DocFolderOperationServiceImpl implements DocFolderOperationService {

    @Resource
    private TyplmSubfolderMapper typlmSubfolderMapper;

    @Resource
    private StoragePlanningMapper storagePlanningMapper;

    @Resource
    private RTTypeClassMapper rtTypeClassMapper;

    @Resource
    private CheckDocMapper checkDocMapper;

    @Resource
    private TyplmWorkFlowContainerService typlmWorkFlowContainerService;

    @Resource
    private TyplmSubFolderLinkService typlmSubFolderLinkService;

    @Resource
    private TyplmSubFilterObjectService typlmSubFilterObjectService;

    @Resource
    private TyplmCommonTableViewService typlmCommonTableViewService;

    @Resource
    private TyplmCommonMoveService typlmCommonMoveService;

    @Resource
    private TyplmHyperlinkService typlmHyperlinkService;

    @Resource
    private TyplmAttributeValueService typlmAttributeValueService;

    @Resource
    private TyplmTypeService typlmTypeService;

    @Resource
    private RTPartMapper rtPartMapper;

    @Resource
    private TyplmUserService typlmUserService;

    @Resource
    private SnowflakeIdComponent snowflakeIdComponent;

    @Resource
    private ObjectStorageService objectStorageService;

    /**
     *
     * 查询是是否有类型与文件夹名称相同的目录
     *
     */
    @Override
    public SubfolderDO obtainTheDirectoryBasedOnItsName(String name,String containerOid) {

        SubfolderDO subfolderDO = new SubfolderDO();
        subfolderDO.setName(name);
        subfolderDO.setParentnodeoid(new BigInteger("0"));
        subfolderDO.setContaineroid(new BigInteger(containerOid));

        SubfolderDO s = typlmSubfolderMapper.selectOne(subfolderDO);
        return s;
    }

    @Override
    public String selectCabinetIdByCabinetId(String oid) {
        return storagePlanningMapper.selectCabinetIdByOid(oid);
    }

    @Override
    public DocxxxxVO selectProjectBycContainerId(String id){
        return storagePlanningMapper.selectProjectBycContainerId(id);
    }

    @Override
    public PDFProjectVO selectPdmIdsByName(String name){
        return storagePlanningMapper.selectPdmIds(name);
    }

    @Override
    public HyperlinkDTO assignmentToHyperlinkDTO(CreateDocInputDTO createDocInputDTO, DocumentVO v) {
        HyperlinkDTO hyperlinkDTO=new HyperlinkDTO();
        String oId=null;
        if (NumberUtil.isNumber(v.getOid())){
            oId=v.getOid();
        }else {
            oId=v.getOid().substring(v.getOid().indexOf(":")+1);
        }
        hyperlinkDTO.setOid(new BigInteger(oId));
        hyperlinkDTO.setOtype(v.getCheckoutinfootype());
        String cId = storagePlanningMapper.selectCabinetIdByOid2(createDocInputDTO.getContainerId());
        hyperlinkDTO.setCabinetoid(new BigInteger(cId));
        hyperlinkDTO.setCabinetotype("ty.inteplm.folder.CTyCabinet");
        hyperlinkDTO.setContaineroid(new BigInteger(createDocInputDTO.getContainerId()));
        hyperlinkDTO.setContainerotype(createDocInputDTO.getContainerType());
        hyperlinkDTO.setBranchid(v.getBranchid());
        hyperlinkDTO.setContainerName(createDocInputDTO.getContainerName());
        hyperlinkDTO.setName(createDocInputDTO.getNumber()+","+createDocInputDTO.getName());
        hyperlinkDTO.setSubfolderoid(new BigInteger(createDocInputDTO.getFolderId()));
        hyperlinkDTO.setSubfolderotype("ty.inteplm.folder.CTySubFolder");
        hyperlinkDTO.setPath(createDocInputDTO.getFolderPath());
        return hyperlinkDTO;

    }

    @Override
    public HyperlinkDTO assignmentToHyperlinkDTO(InsertPartDTO insertPartDTO, PdmPartInfoVO v) {
        HyperlinkDTO hyperlinkDTO=new HyperlinkDTO();

        hyperlinkDTO.setOid(v.getOid());
        hyperlinkDTO.setOtype(v.getCheckoutinfootype());
        String cId = storagePlanningMapper.selectCabinetIdByOid2(insertPartDTO.getContainerID());
        hyperlinkDTO.setCabinetoid(new BigInteger(cId));
        hyperlinkDTO.setCabinetotype("ty.inteplm.folder.CTyCabinet");
        hyperlinkDTO.setContaineroid(new BigInteger(insertPartDTO.getContainerID()));
        hyperlinkDTO.setContainerotype(insertPartDTO.getContainerType());
        hyperlinkDTO.setBranchid(v.getBranchid());
        hyperlinkDTO.setContainerName(insertPartDTO.getContainerName());
        hyperlinkDTO.setName(insertPartDTO.getPartNumber()+","+insertPartDTO.getName());
        hyperlinkDTO.setSubfolderoid(new BigInteger(insertPartDTO.getFolderId()));
        hyperlinkDTO.setSubfolderotype("ty.inteplm.folder.CTySubFolder");
        hyperlinkDTO.setPath(insertPartDTO.getFolderPath());
        return hyperlinkDTO;

    }

    @Override
    public HyperlinkDTO assignmentToHyperlinkDTO(InsertCadDocDTO cadDocCreateDTO, String oid) {
        HyperlinkDTO hyperlinkDTO=new HyperlinkDTO();

        hyperlinkDTO.setOid(new BigInteger(oid));
        hyperlinkDTO.setOtype("ty.inteplm.cad.CTyCADDoc");
        String cId = storagePlanningMapper.selectCabinetIdByOid2(cadDocCreateDTO.getContainerId());
        hyperlinkDTO.setCabinetoid(new BigInteger(cId));
        hyperlinkDTO.setCabinetotype("ty.inteplm.folder.CTyCabinet");
        hyperlinkDTO.setContaineroid(new BigInteger(cadDocCreateDTO.getContainerId()));
        hyperlinkDTO.setContainerotype(cadDocCreateDTO.getContainerType());
        String branchId = storagePlanningMapper.selectCadDocBranchIdByOid(oid);
        hyperlinkDTO.setBranchid(new BigInteger(branchId));
        hyperlinkDTO.setContainerName(cadDocCreateDTO.getContainerName());
        hyperlinkDTO.setName(cadDocCreateDTO.getNumber()+","+cadDocCreateDTO.getName());
        hyperlinkDTO.setSubfolderoid(new BigInteger(cadDocCreateDTO.getFolderId()));
        hyperlinkDTO.setSubfolderotype("ty.inteplm.folder.CTySubFolder");
        hyperlinkDTO.setPath(cadDocCreateDTO.getFolderPath());
        return hyperlinkDTO;
    }



    @Override
    public void deliverablesMovedWarehouse(String workFlowContainerId) {
        WorkFlowContainerView workFlowContainerDetailByPrimaryKey = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(
                new BigInteger(workFlowContainerId));
        if (!workFlowContainerDetailByPrimaryKey.getContainerOtype().equals("ty.inteplm.project.CTyProject")){
            return;
        }
        String cabinetId = storagePlanningMapper.selectCabinetIdByOid2(workFlowContainerDetailByPrimaryKey.getContainerOid().toString());
        List<SubfolderWebVO> subfolderWebVOS = typlmSubFolderLinkService.querySubFolderListByParent(
                new BigInteger(cabinetId), "ty.inteplm.folder.CTyCabinet");
        Iterator<SubfolderWebVO> iterator = subfolderWebVOS.iterator();
        List<TyplmFolderObjDTO> list=new ArrayList<>();

        TableViewDescriptorVO tableViewDescriptorDetail = typlmCommonTableViewService.getTableViewDescriptorDetail(
                new IdentifierEntity(new BigInteger("363292770112602112"), "ty.inteplm.folder.CTyTableViewDescriptor"));
        //遍历文件夹
        while (iterator.hasNext()){
            SubfolderWebVO subfolderWebVO = iterator.next();
            Callback(list,subfolderWebVO,tableViewDescriptorDetail);
        }
        Iterator<TyplmFolderObjDTO> typlmFolderObjDTOIterator = list.iterator();


        BatchMoveOidParamDTO batchMoveOidParamDTO=new BatchMoveOidParamDTO();
        batchMoveOidParamDTO.setUpdateAllVersion(true);
        List<MoveOidParamDTO> moveList=new ArrayList<>();
        List<HyperlinkDTO> hyperlinkList=new ArrayList<>();

        while (typlmFolderObjDTOIterator.hasNext()){
            TyplmFolderObjDTO typlmFolderObjDTO = typlmFolderObjDTOIterator.next();
            ObjectStorageDO objectStorageDO = objectStorageService.selectOneByTypeDefName(typlmFolderObjDTO.getTypeDefName());

            //判断枚举中是否存在对应关系
            if(ObjectUtil.isNotEmpty(objectStorageDO)){
                PDFProjectVO pdfProjectVO = storagePlanningMapper.selectPdmIds(objectStorageDO.getContextName());
                //判断产品库是否存在
                if (ObjectUtil.isNotEmpty(pdfProjectVO)){
                    String folderId = null;

                    String[] split = objectStorageDO.getFolderPath().split("/");
                    for (int i = 0; i < split.length; i++) {
                        if (i==0){
                            folderId= storagePlanningMapper.selectOidByNameAndCid(pdfProjectVO.getOId(),
                                                                                  split[i], "0");
                        }else {
                            folderId= storagePlanningMapper.selectOidByNameAndCid(pdfProjectVO.getOId(),
                                                                                  split[i], folderId);
                        }
                        if (StrUtil.isBlank(folderId)){
                            //创建文件夹
                            SubfolderVO subfolder=new SubfolderVO();
                            subfolder.setName(split[i]);
                            subfolder.setDescription("--系统自动创建--文件夹--");
                            subfolder.setParentOid(i==0?pdfProjectVO.getDefaultcabinetoid():folderId);
                            subfolder.setParentOtype(i==0?"ty.inteplm.folder.CTyCabinet":"ty.inteplm.folder.CTySubFolder");
                            SubfolderWebVO subfolderWebVO = typlmSubFolderLinkService.createSubfolder(subfolder);
                            folderId=subfolderWebVO.getOid();
                        }
                    }

                    IdentifierEntity entityIdentifier = new IdentifierEntity(typlmFolderObjDTO.getOid(),typlmFolderObjDTO.getOtype());
                    IdentifierEntity folderIdentifier = new IdentifierEntity(new BigInteger(folderId),"ty.inteplm.folder.CTySubFolder");
                    moveList.add(new MoveOidParamDTO(entityIdentifier,folderIdentifier));

                    HyperlinkDTO hyperlinkDTO=new HyperlinkDTO();
                    hyperlinkDTO.setOid(typlmFolderObjDTO.getOid());
                    hyperlinkDTO.setBranchid(typlmFolderObjDTO.getBranchid());
                    hyperlinkDTO.setOtype(typlmFolderObjDTO.getOtype());
                    hyperlinkDTO.setCabinetoid(new BigInteger(cabinetId));
                    hyperlinkDTO.setCabinetotype("ty.inteplm.folder.CTyCabinet");
                    hyperlinkDTO.setContaineroid(workFlowContainerDetailByPrimaryKey.getContainerOid());
                    hyperlinkDTO.setContainerotype(workFlowContainerDetailByPrimaryKey.getContainerOtype());
                    hyperlinkDTO.setContainerName(workFlowContainerDetailByPrimaryKey.getContainerName());
                    hyperlinkDTO.setName(typlmFolderObjDTO.getNumber()+","+typlmFolderObjDTO.getName());
                    hyperlinkDTO.setSubfolderoid(typlmFolderObjDTO.getSubfolderoid());
                    hyperlinkDTO.setSubfolderotype("ty.inteplm.folder.CTySubFolder");
                    hyperlinkDTO.setPath("/"+typlmFolderObjDTO.getContextName()+"/");
                    hyperlinkList.add(hyperlinkDTO);
                }
            }
        }
        batchMoveOidParamDTO.setMoveList(moveList);

        DocFolderOperationService docFolderOperationService = SpringUtil.getBean(DocFolderOperationService.class);
        //修改项目为非密
        String attributesID = storagePlanningMapper.queryExtendedAttributesID(
                workFlowContainerDetailByPrimaryKey.getContainerOid().toString(), "密级");


        //项目解密
        docFolderOperationService.ModifyTheValueOfTheExtendedAttribute(workFlowContainerDetailByPrimaryKey,attributesID);
        //交付物解密
        deliverableDecryption(list);
        //创建文件超链接
        typlmHyperlinkService.batchCreateHyperlink(hyperlinkList);
        //移动原文件
        typlmCommonMoveService.moveObjects(batchMoveOidParamDTO);

    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void ModifyTheValueOfTheExtendedAttribute(WorkFlowContainerView workFlowContainerDetailByPrimaryKey,String attributesID) {
        String result = storagePlanningMapper.selectValExist(workFlowContainerDetailByPrimaryKey.getContainerOid().toString(),
                                                             attributesID);
        if (StrUtil.isNotBlank(result) && NumberUtil.isInteger(result) && Integer.parseInt(result) > 0){
            storagePlanningMapper.ModifyTheValueOfTheExtendedAttribute(workFlowContainerDetailByPrimaryKey.getContainerOid().toString(), attributesID, "非密");
        }else {
            String oid = snowflakeIdComponent.getInstance().nextId().toString();
            storagePlanningMapper.insertAttVal(attributesID, workFlowContainerDetailByPrimaryKey.getContainerOid().toString(), oid, "非密");
        }
    }
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void deliverableDecryption(List<TyplmFolderObjDTO> list){
        for (TyplmFolderObjDTO typlmFolderObjDTO:list){
            if(typlmFolderObjDTO.getOtype().equals("ty.inteplm.doc.CTyDoc")){
                storagePlanningMapper.updateDocSecuritylabels(typlmFolderObjDTO.getOid().toString());
            } else if (typlmFolderObjDTO.getOtype().equals("ty.inteplm.part.CTyPart")) {
                storagePlanningMapper.updatePartSecuritylabels(typlmFolderObjDTO.getOid().toString());
            } else if(typlmFolderObjDTO.getOtype().equals("ty.inteplm.cad.CTyCADDoc")){
                storagePlanningMapper.updateCadDocSecuritylabels(typlmFolderObjDTO.getOid().toString());
            }
        }
    }

    @Override
    public List<ModelStatisticsVO> queryDifferentModelsUnderTheEBOMNumber() {
        return storagePlanningMapper.queryDifferentModelsUnderTheEBOMNumber();
    }

    @Override
    public List<EBOMVO> ebomQuery(String type) {
        List<String> ibaNames = queryIbaName(type);
        if(CollUtil.isEmpty(ibaNames)){
            return null;
        }

        int i = type.indexOf("|");
        type = type.substring(i+1);

        List<PartNumberDTO> partDOS = rtPartMapper.selectAllPart(type);
        if(CollUtil.isEmpty(partDOS)){
            return null;
        }
        List<BigInteger> partOidList = partDOS.stream().map(PartNumberDTO::getOid).collect(Collectors.toList());
        Map<String, List<AttributeVO>> attrValueByOidAndKeys = typlmAttributeValueService.getIbaAttrValueByOidAndKeys(
                partOidList,
                TableTypeConstrant.TY_PART_OTYPE,
                ibaNames);
        List<EBOMVO> ebomList=new ArrayList<>();
        List<LifeCycleStateDO> lifeCycleStateDOS = storagePlanningMapper.selectLifeCycleStateEnum();

        for (PartNumberDTO partDO : partDOS) {
            EBOMVO vo = new EBOMVO();
            vo.setOid(String.valueOf(partDO.getOid()));
            vo.setOtype(partDO.getOtype());
            List<AttributeVO> attributeVOS = attrValueByOidAndKeys.get(String.valueOf(partDO.getOid()));
            vo.setCode(partDO.getPartNumber());
            vo.setCodeName(partDO.getName());

            UserDO creatorUserDO = typlmUserService.queryUserByKey(partDO.getCreatoroId());
            vo.setCreator(creatorUserDO.getFullname());
            vo.setCreateTime(partDO.getCreatestamp());
            vo.setModifyTime(partDO.getUpdatestamp());

            UserDO modifierUserDO = typlmUserService.queryUserByKey(partDO.getUpdatoroId());
            vo.setModifier(modifierUserDO.getFullname());
            vo.setDeactivationTime(partDO.getLastupdatestamp());
            vo.setComponentCode(partDO.getPartNumber().substring(0,5));
            vo.setActiveFlag(partDO.getLifecyclestagekey());

            if (partDO.getPartNumber().length() > 12){
                vo.setDrawingCode(partDO.getPartNumber().substring(0,13));
            }

            if (CollUtil.isNotEmpty(attributeVOS)) {
                for (AttributeVO attributeVO : attributeVOS) {
                    //开发令
                    if ("DevelopmentOrder".equals(attributeVO.getAttributeEnName())) {
                        String developmentOrder = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setDevelopmentOrder(developmentOrder);
                    }
                    //机型
                    else if ("productType".equals(attributeVO.getAttributeEnName())) {
                        String productType = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setProductType(productType);
                    }
                    //v2机型
                    else if ("productTypeV2".equals(attributeVO.getAttributeEnName())) {
                        String productTypeV2 = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setProductTypeV2(productTypeV2);
                    }//推荐级别
                    else if ("RecommendLevel".equals(attributeVO.getAttributeEnName())) {
                        String RecommendLevel = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setRecommendLevel(RecommendLevel);
                    }//认证
                    else if ("certificationType".equals(attributeVO.getAttributeEnName())) {
                        String certificationType = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setCertificationType(certificationType);
                    }//公司备案机型
                    else if ("companyModel".equals(attributeVO.getAttributeEnName())) {
                        String companyModel = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setCompanyModel(companyModel);
                    }//客户备案机型
                    else if ("customerModel".equals(attributeVO.getAttributeEnName())) {
                        String customerModel = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setCustomerModel(customerModel);
                    }//开发项目
                    else if ("developmentProject".equals(attributeVO.getAttributeEnName())) {
                        String developmentProject = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setDevelopmentProject(developmentProject);
                    }//备注
                    else if ("REMARK".equals(attributeVO.getAttributeEnName())) {
                        String remark = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setREMARK(remark);
                    }//半bom
                    else if ("HalfBOM".equals(attributeVO.getAttributeEnName())) {
                        String HalfBOM = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setHalfBOM(HalfBOM);
                    }//是否已批量
                    else if ("SFBatch".equals(attributeVO.getAttributeEnName())) {
                        String SFBatch = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setSFBatch(SFBatch);
                    }//需小批
                    else if ("NeedBatch".equals(attributeVO.getAttributeEnName())) {
                        String NeedBatch = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setNeedBatch(NeedBatch);
                    }//越南生产
                    else if ("MadeVietnam".equals(attributeVO.getAttributeEnName())) {
                        String MadeVietnam = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setMadeVietnam(MadeVietnam);
                    }//样机
                    else if ("ModelMachine".equals(attributeVO.getAttributeEnName())) {
                        String ModelMachine = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setModelMachine(ModelMachine);
                    }//描述修改意见
                    else if ("DescriptionModificationSuggestions".equals(attributeVO.getAttributeEnName())) {
                        String pro = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setDescriptionModificationSuggestions(pro);
                    }
                    //拼接描述
                    else if ("description".equals(attributeVO.getAttributeEnName())) {
                        String pro = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setDescription(pro);
                    }//主推机型
                    else if ("MainRecommendedModels".equals(attributeVO.getAttributeEnName())) {
                        String pro = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setMainRecommendedModels(pro);
                    }//主推状态
                    else if ("MainPushStatus".equals(attributeVO.getAttributeEnName())) {
                        String pro = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setMainPushStatus(pro);
                    }//历史描述
                    else if ("historyDescription".equals(attributeVO.getAttributeEnName())) {
                        String pro = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setHistoryDescription(pro);
                    }//开发令
                    else if ("devCode".equals(attributeVO.getAttributeEnName())) {
                        String pro = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setDevCode(pro);
                    }//机型号
                    else if ("productType".equals(attributeVO.getAttributeEnName())) {
                        String pro = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setProductType(pro);
                    }//产品型谱名称
                    else if ("ProductSpectrumName".equals(attributeVO.getAttributeEnName())) {
                        String pro = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setProductSpectrumName(pro);
                    }//产品小类
                    else if ("ProductSeries".equals(attributeVO.getAttributeEnName())) {
                        String pro = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setProductSeries(pro);
                    }
                    else if ("ProductSpectrumName".equals(attributeVO.getAttributeEnName())) {
                        String pro = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setDescription(pro);
                    }
                    else if ("HistoricalModels".equals(attributeVO.getAttributeEnName())) {
                        String pro = StringUtils.isBlank(attributeVO.getAttributeValuex()) ? attributeVO.getAttributeValue()
                                : attributeVO.getAttributeValuex();
                        vo.setDescription(pro);
                    }

                    if (vo.getProductSpectrumName()!=null && vo.getHistoricalModels()!=null){
                        vo.setSplicing(vo.getProductSpectrumName()+vo.getHistoricalModels());
                    }
                }
            }
            Optional<LifeCycleStateDO> lifeCycleStateDO = lifeCycleStateDOS.stream().filter(
                    f -> f.getName().equals(vo.getActiveFlag())).findAny();
            if (lifeCycleStateDO.isPresent()){
                vo.setActiveFlag(lifeCycleStateDO.get().getDisplayName());
            }else {
                vo.setActiveFlag("");
            }
            ebomList.add(vo);
        }


        return ebomList;
    }

    @Override
    public String checkDocByName(List<Document> documentList) {
        StringBuilder sb = new StringBuilder();
        boolean flag = false;
        for(Document document : documentList){
            String name = document.getName();
            BigInteger typeoid = document.getTypeoid();
            name = name.contains(".") ? name.substring(0, name.lastIndexOf('.')) : name;
            List<JSONObject> docList = checkDocMapper.queryDocName(name, typeoid);
            List<String> list = new ArrayList<>();
            for(JSONObject json : docList){
                String lifecyclestageKey = json.getString("LIFECYCLESTAGEKEY");
                if("Creating".equals(lifecyclestageKey)){
                    list.add(json.getString(name));
                }else {
                    sb.append("文档:").append(json.getString("DOCNUMBER")).append("的状态非创建中!");
                    flag = true;
                }
            }
            if(flag){
                sb.append("不允许创建");
            }else {
                if(CollUtil.isNotEmpty(list)){
                    sb.append("存在相同名称的文档:").append(name).append(",是否继续创建?");
                }
            }
        }
        return sb.toString();
    }

    public List<String> queryIbaName(String type) {
        BigInteger typeOid = rtTypeClassMapper.queryTypeOidByName(type);
        if(typeOid == null){
            return Collections.emptyList();
        }
        List<TyAttributeDefMapVO> tyAttributeDefMapVOS = typlmTypeService.queryInheritAttrDef(typeOid, TableTypeConstrant.TY_TYPE_OTYPE);
        if(CollUtil.isNotEmpty(tyAttributeDefMapVOS)){
            return tyAttributeDefMapVOS.stream().map(TyAttributeDefMapVO::getName).collect(
                    Collectors.toList());
        }
        return Collections.emptyList();
    }

    public void Callback(List<TyplmFolderObjDTO> list,SubfolderWebVO subfolderWebVO,TableViewDescriptorVO tableViewDescriptorDetail){

        List<SubfolderWebVO> subfolderWebVOS = typlmSubFolderLinkService.querySubFolderListByParent(
                new BigInteger(subfolderWebVO.getOid()), "ty.inteplm.folder.CTySubFolder");

        TySubFolderFilterQuery tySubFolderFilterQuery = new TySubFolderFilterQuery();
        tySubFolderFilterQuery.setSubFolderOid(new BigInteger(subfolderWebVO.getOid()));
        tySubFolderFilterQuery.setSubFolderOtype("ty.inteplm.folder.CTySubFolder");
        tySubFolderFilterQuery.setNeedCount(true);
        tySubFolderFilterQuery.setCurPage(1);
        tySubFolderFilterQuery.setPageSize(999);
        TySubFolderFilterDTO tySubFolderFilterDTO = new TySubFolderFilterDTO();
        tySubFolderFilterDTO.setName("全部");
        tySubFolderFilterDTO.setPublished(1l);
        tySubFolderFilterDTO.setHidden(0l);
        tySubFolderFilterDTO.setKeyWords("");
        tySubFolderFilterDTO.setOtype("ty.inteplm.folder.CTyTableViewDescriptor");
        tySubFolderFilterDTO.setSearchColumnList(tySubFolderFilterDTO.getSearchColumnList());
        tySubFolderFilterDTO.setOid(new BigInteger("363292770112602112"));
        tySubFolderFilterDTO.setSearchCondition(BeanUtil.copyProperties(tableViewDescriptorDetail.getSearchCondition(), SearchConditionDTO.class,"showAllViewVersion","tableId","excludeTypeInthidList","showAllViewVersion"));
        tySubFolderFilterQuery.setSubFolderFilterDTO(tySubFolderFilterDTO);

        List<TyplmFolderObjDTO> dtos = typlmSubFilterObjectService.getFolderObjectByFilter(tySubFolderFilterQuery)
                .getList();

        if (CollUtil.isNotEmpty(dtos)){
            list.addAll(dtos);
        }

        if (CollUtil.isEmpty(subfolderWebVOS)){
            return;
        }

        Iterator<SubfolderWebVO> iterator = subfolderWebVOS.iterator();
        while (iterator.hasNext()){
            SubfolderWebVO vo = iterator.next();
            Callback(list,vo,tableViewDescriptorDetail);
        }

    }
}
